package com.game.gameHandle
{
	import com.game.gameObject.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	
	import mx.collections.*;
	import mx.containers.Canvas;
	import mx.core.*;
	
	public class GameObjectManager {
		//背景缓存
		public var backBuffer:BitmapData;
		//背景颜色
		public var clearColor:uint = 0xFF849AA5;
		//实例化对象，单例模式
		protected static var instance:GameObjectManager = null;
		//最后帧时，为了得到每个帧相隔秒数
		protected var lastFrame:Date;
		//需要加入的基本游戏对象
		protected var newBaseObjects:ArrayCollection = new ArrayCollection();
		//需要移除的基本游戏对象
		protected var removedBaseObjects:ArrayCollection = new ArrayCollection();
		
		protected var baseObjects:ArrayCollection = new ArrayCollection();
		
		public var curUser:Player;
		
		public function GameObjectManager() 
		{
			if ( instance != null )
				throw new Error( "只能初始化一个对象" ); 
		}
		
		//单例模式
		static public function get Instance():GameObjectManager 
		{
			if ( instance == null )
				instance = new GameObjectManager();
			return instance;
		}
		
		//初始化背景缓存
		public function setBackBuffer(myCanvas:Canvas):void {
			backBuffer = new BitmapData(myCanvas.width, myCanvas.height, false);
		}
		
		//计算初始的帧时
		public function startup():void 
		{
			lastFrame = new Date();	
		}
		
		//进入物件的每个帧
		public function enterFrame():void 
		{
			// 计算两帧的相隔秒数
			var thisFrame:Date = new Date();
			var seconds:Number = (thisFrame.getTime() - lastFrame.getTime())/1000.0;
	    	lastFrame = thisFrame;
	    	
	    	//得到需要使用的物件
	    	removeDeletedBaseObjects();
	    	insertNewBaseObjects();
	    		    	
			for each (var gameObject:BaseObject in baseObjects)
			if (gameObject.inuse) 
				gameObject.enterFrame(seconds);  
 	
 			//画物件及背景
	    	drawObjects();
		}
		
		public function click(event:MouseEvent):void
		{
			for each (var gameObject:BaseObject in baseObjects)
				if (gameObject.inuse) 
					gameObject.click(event);
			//curUser.click(event);
			
		}
		
		protected function drawObjects():void 
		{
			//填充背景缓存为蓝色矩形区域
			backBuffer.fillRect(backBuffer.rect, clearColor);
			
			//画所有对象
			for each (var baseObject:BaseObject in baseObjects)
				if (baseObject.inuse)
					baseObject.copyToBackBuffer(backBuffer);
		}
		
		public function addBaseObject(baseObject:BaseObject):void
		{
			newBaseObjects.addItem(baseObject);
		}
		
		public function removeBaseObject(baseObject:BaseObject):void
		{
			removedBaseObjects.addItem(baseObject);
		}
		
		//加入需要加入的物件
		protected function insertNewBaseObjects():void
		{
			for each (var baseObject:BaseObject in newBaseObjects)
			{
				for (var i:int = 0; i < baseObjects.length; ++i)
				{
					var otherBaseObject:BaseObject = baseObjects.getItemAt(i) as BaseObject;
					
					if (otherBaseObject.zOrder > baseObject.zOrder ||
						otherBaseObject.zOrder == -1)
						break;
				}

				baseObjects.addItemAt(baseObject, i);
			}
			
			newBaseObjects.removeAll();
		}
		
		//删除需要删除的物件
		protected function removeDeletedBaseObjects():void
		{
			
			for each (var removedObject:BaseObject in removedBaseObjects)
			{
				var i:int = 0;
				for (i = 0; i < baseObjects.length; ++i)
				{
					if (baseObjects.getItemAt(i) == removedObject)
					{
						baseObjects.removeItemAt(i);
						break;
					}
				}
				
			}
			
			removedBaseObjects.removeAll();
		}
	}
}